package com.iot.ic.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {
	
	
	public static final String TIME_WITH_MINUTE_PATTERN = "HH:mm";

	public static final long DAY_MILLI = 24 * 60 * 60 * 1000; // 一天的MilliSecond

	public final static int LEFT_OPEN_RIGHT_OPEN = 1;
	public final static int LEFT_CLOSE_RIGHT_OPEN = 2;
	public final static int LEFT_OPEN_RIGHT_CLOSE = 3;
	public final static int LEFT_CLOSE_RIGHT_CLOSE = 4;
	/**
	 * 比较日期的模式 --只比较日期，不比较时间
	 */
	public final static int COMP_MODEL_DATE = 1;
	/**
	 * 比较日期的模式 --只比较时间，不比较日期
	 */
	public final static int COMP_MODEL_TIME = 2;
	/**
	 * 比较日期的模式 --比较日期，也比较时间
	 */
	public final static int COMP_MODEL_DATETIME = 3;



	/**
	 * 要用到的DATE Format的定义
	 */
	public static String DATE_FORMAT_DATEONLY = "yyyy-MM-dd"; // 年/月/日
	public static String DATE_FORMAT_DATETIME = "yyyy-MM-dd HH:mm:ss"; // 年/月/日
	
	public static final SimpleDateFormat SHORTDATEFORMAT = new SimpleDateFormat("yyyyMMdd");
	public static final SimpleDateFormat SHORT_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	public static final SimpleDateFormat LONG_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static final SimpleDateFormat HMS_FORMAT = new SimpleDateFormat("HH:mm:ss");
	public static final SimpleDateFormat formatTimestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * @return 返回系统当前时间
	 */
	public static Date getCurrentDate(){
		
	  return Calendar.getInstance().getTime() ;
		
	}
	
	/**
	 * 利用缺省的Date格式(YYYY/MM/DD)转换String到java.sql.Timestamp
	 * 
	 * @param sDate
	 *            Date string
	 * @return
	 * @since 1.0
	 * @history
	 */
	
	/**
	 * 根据格式获取当前时间
	 * @param format
	 * @return
	 */
	public static String getTimeOfNow(String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);
		return sdf.format(new Date());
	}
	/**
	 * 根据格式获取本周一的开始时间
	 * 
	 * @return
	 */
	public static String getFirstDayOfWeek(String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.setTimeInMillis(System.currentTimeMillis());
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return sdf.format(cal.getTime());
	}
	
	/**
	 * 根据格式获取本周末的结束时间
	 * @return
	 */
	public static String getLastDayOfWeek(String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		cal.setFirstDayOfWeek(Calendar.MONDAY);
		cal.setTimeInMillis(System.currentTimeMillis());
		cal.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
		return sdf.format(cal.getTime());
	}
	
	/**
	 * 时间格式的转换
	 * @param dateStr
	 * @param oldFormat
	 * @param newFormat
	 * @return
	 * @throws ParseException
	 */
	public static String reverseDateFormat(String dateStr,String oldFormat,String newFormat) throws ParseException{
		Date date = new SimpleDateFormat(oldFormat,Locale.CHINA).parse(dateStr);
		return new SimpleDateFormat(newFormat,Locale.CHINA).format(date);
	}
	/**
	 * 将当天时间加上或减去若干天  按格式返回
	 * @param format 格式
	 * @param num  天数
	 * @return
	 */
	public static String getAfterDay(String format,int num){
		SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		cal.add(Calendar.DAY_OF_WEEK, num);
		return sdf.format(cal.getTime());
	}
	/**
	 * 将特定时间加上或减去若干天  按格式返回
	 * @param format 格式
	 * @param num  天数
	 * @return
	 */
	public static String getAfterSpecificDay(String timeStr,String format,int num){
		SimpleDateFormat sdf=new SimpleDateFormat(format);
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		Date date=null;
		try {
			date = sdf.parse(timeStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_WEEK, num);
		return sdf.format(cal.getTime());
	}
	
	/**
	 * 将当天时间加上或减去若干月  按格式返回
	 * @param format 格式
	 * @param num  天数
	 * @return
	 */
	public static String getAfterMonth(String format,int num){
		SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		cal.add(Calendar.MONTH, num);
		return sdf.format(cal.getTime());
	}
	
	/**
	 * 将当天时间加上或减去若干年  按格式返回
	 * @param format 格式
	 * @param num  天数
	 * @return
	 */
	public static String getAfterYear(String format,int num){
		SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.CHINA);
		Calendar cal = Calendar.getInstance(Locale.CHINA);
		cal.add(Calendar.YEAR, num);
		return sdf.format(cal.getTime());
	}
	
	/**
	 * 比较2个日期字符串的大小   
	 * @param dateStr1
	 * @param dateStr2
	 * @param format1
	 * @param format2
	 * @return  1：dateStr1>dateStr2
	 * 			0: dateStr1=dateStr2
	 * 		   -1: dateStr1<dateStr2
	 */
	public static int dateStringCompare(String dateStr1,String dateStr2,String format1,String format2){
		SimpleDateFormat sdf1 = new SimpleDateFormat(format1,Locale.CHINA);
		SimpleDateFormat sdf2 = new SimpleDateFormat(format2,Locale.CHINA);
		Calendar cal1 = Calendar.getInstance(Locale.CHINA);
		Calendar cal2 = Calendar.getInstance(Locale.CHINA);
		Date date1 = null;
		Date date2 = null;
		try {
			date1 = sdf1.parse(dateStr1);
			date2 = sdf2.parse(dateStr2);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		cal1.setTime(date1);
		cal2.setTime(date2);
		return cal1.compareTo(cal2); 
	}
	
	/**
	 * 计算2个日期的间隔天数
	 * @param t1
	 * @param t2
	 * @return
	 * @throws ParseException
	 */
	public static int getBetweenDays(String t1,String t2,String format1,String format2) { 
		SimpleDateFormat sdf1 = new SimpleDateFormat(format1); 
		SimpleDateFormat sdf2 = new SimpleDateFormat(format2); 
		int betweenDays = 0; 
		Date d1 = null;
		Date d2 = null;
		try {
			d1 = sdf1.parse(t1);
			d2 = sdf2.parse(t2); 
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		Calendar c1 = Calendar.getInstance(); 
		Calendar c2 = Calendar.getInstance(); 
		c1.setTime(d1); 
		c2.setTime(d2); 
		// 保证第二个时间一定大于第一个时间 
		if(c1.after(c2)){ 
			c1.setTime(d2);
			c2.setTime(d1); 
		} 
		int betweenYears = c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR); 
		betweenDays = c2.get(Calendar.DAY_OF_YEAR)-c1.get(Calendar.DAY_OF_YEAR); 
		for(int i=0;i<betweenYears;i++){ 
			c1.set(Calendar.YEAR,(c1.get(Calendar.YEAR)+1)); 
			betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR); 
		} 
		return betweenDays+1; 
	}

	/**
	 * 获取指定月份的天数
	 * @param yearMonth 年月
	 * @param format 格式
     * @return
     */
	public static int getDaysOfMonth(String yearMonth,String format){
		int dayNum = 0;
		SimpleDateFormat sdf = new SimpleDateFormat(format); 
		Date date = null;
		try {
			date = sdf.parse(yearMonth);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Calendar cal = Calendar.getInstance(); 
		cal.setTime(date);
		dayNum = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
		return dayNum;
	}
	
	/**
	 * 取得指定日期格式的字符串
	 * 
	 * @param Date
	 * @return String
	 */
	public static String formatDate(Date date, String format) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(format);
		return dateFormat.format(date);
	}
	
}
